/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.genapha.forms.GeneGeneInteractionForm;
import ca.ubc.icapture.genapha.forms.PathwayForm;
import ca.ubc.icapture.genapha.forms.SearchAssociationForm;
import icapture.SQLMgr;
import icapture.beans.DB.SNP;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

/**
 *
 * @author BTripp
 */
public class PathwayAction extends Action
{

    @Override
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm Form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    {
        HttpSession session = request.getSession();
        PathwayForm form = (PathwayForm) Form;

        if (form.getSubmitType().equalsIgnoreCase("SNP List"))
        {
            StringBuilder sb = new StringBuilder();
            if (form.getGeneNames().length > 0)
            {
                sb.append(form.getGeneNames()[0]);
                for (int i = 1; i < form.getGeneNames().length; i++)
                {
                    sb.append("\n" + form.getGeneNames()[i]);
                }
            }

            // Using ActionRedirect class to pass data to SearchAction.
            ActionRedirect redirect = new ActionRedirect(mapping.findForward("snpList"));
            redirect.addParameter("genes", sb.toString());
            redirect.addParameter("searchSelect", 1);
            redirect.addParameter("submitType", "Search");

            return redirect;
        }
        else if (form.getSubmitType().equalsIgnoreCase("Associations"))
        {
            StringBuilder sb = new StringBuilder();
            if(form.getGeneNames().length > 0){
                sb.append(form.getGeneNames()[0]);
                for(int i=1;i<form.getGeneNames().length;i++){
                    sb.append("\n" + form.getGeneNames()[i]);
                }
            }

            // Create a search association form.
            // Pass the data over and add the form to the session.
            SearchAssociationForm assocForm = new SearchAssociationForm();
            session.setAttribute("searchAssociationForm", assocForm);
            assocForm.setSelectedCohorts(new String[] {"NHL"});
            assocForm.setSelectedPhenotypes(new String[] {"NHL"});
            assocForm.setChkGene(true);
            assocForm.setGenes(sb.toString());
            assocForm.setSubmitType("Search");

            return mapping.findForward("associationSearch");
        }
        else if (form.getSubmitType().equalsIgnoreCase("Gene Gene Interaction"))
        {
            ArrayList<SNP> snpList = new ArrayList<SNP>();
            if (form.getGeneNames().length > 0)
            {
                for (int i = 0; i < form.getGeneNames().length; i++)
                {
                    snpList.addAll(SQLMgr.getSNPs(SQLMgr.getGene(form.getGeneNames()[i])));
                }
            }

            // Create a gene-gene interaction form.
            // Pass the data over and add the form to the session.
            GeneGeneInteractionForm geneGeneForm = new GeneGeneInteractionForm();
            geneGeneForm.setSnps(snpList);
            session.setAttribute("geneGeneInteractionForm", geneGeneForm);

            return mapping.findForward("gene-gene");
        }

        // Something went wrong if you reached here
        return null;
    }
}